package com.reone.xiaozu.core.util

import android.content.Context
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory
import com.bumptech.glide.load.engine.cache.LruResourceCache
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator
import com.bumptech.glide.module.AppGlideModule

/**
 * glide构建类
 * Created by wangxingsheng on 2017/11/1.
 */

@GlideModule
class MyAppGlideModule : AppGlideModule(){
    private var diskSize = 1024 * 1024 * 100
    private var memorySize = Runtime.getRuntime().maxMemory() / 8  // 取1/8最大内存作为最大缓存

    override fun applyOptions(context: Context, builder: GlideBuilder) {
        super.applyOptions(context, builder)
        // 定义缓存大小和位置
        builder.setDiskCache(InternalCacheDiskCacheFactory(context, diskSize.toLong()))  //内存中

        // 默认内存和图片池大小
        val calculator = MemorySizeCalculator.Builder(context).build()
        val defaultMemoryCacheSize = calculator.memoryCacheSize.toLong() // 默认内存大小
        val defaultBitmapPoolSize = calculator.bitmapPoolSize.toLong() // 默认图片池大小
        builder.setMemoryCache(LruResourceCache(defaultMemoryCacheSize)) // 该两句无需设置，是默认的
        builder.setBitmapPool(LruBitmapPool(defaultBitmapPoolSize))

        // 自定义内存和图片池大小
        builder.setMemoryCache(LruResourceCache(memorySize))
        builder.setBitmapPool(LruBitmapPool(memorySize))

    }
}
